import fs from 'fs'
import path from 'path'
import { fileURLToPath } from 'url'

// 获取当前脚本所在目录
const __filename = fileURLToPath(import.meta.url)
const __dirname = path.dirname(__filename)

console.log('Converting .js to .ts in CURRENT FOLDER only...')

// 1. 读取当前目录的所有文件
const files = fs.readdirSync(__dirname)

// 2. 过滤出.js文件（排除自身脚本和node_modules）
const jsFiles = files.filter(
  (file) =>
    file.endsWith('.js') && file !== path.basename(__filename) && !file.includes('node_modules')
)

// 3. 逐个重命名
jsFiles.forEach((file) => {
  const oldPath = path.join(__dirname, file)
  const newPath = path.join(__dirname, file.replace('.js', '.ts'))

  try {
    fs.renameSync(oldPath, newPath)
    console.log(`✅ Renamed: ${file} → ${file.replace('.js', '.ts')}`)
  } catch (err) {
    console.error(`❌ Failed to rename ${file}:`, err.message)
  }
})

console.log(`Conversion complete! Processed ${jsFiles.length} files.`)
